<template>
	<view class="content">
		<view>
			
			<view class="u-flex user-box  u-p-t-20 u-p-l-30 u-p-r-20 u-p-b-30">
				<view class="u-m-r-10">
					<u-avatar :src="avatars" size="140"></u-avatar>
				</view>
				<view class="u-flex-1">
					<view class="u-font-18 u-p-b-20" >{{name}}</view>
					<view class="u-font-14 u-tips-color">account:{{username}}</view>
				</view>
			</view>
			
			<view class="u-m-t-20" v-if="onupdates">
				<u-cell-group>
					<u-cell-item @click="onupdatess()"  title="Check for Updates"></u-cell-item>
					<u-cell-item @click="jumpOld()"  title="Bluetooth Connection"></u-cell-item>
					<!-- <u-cell-item @click="lydzc()"  title="Bluetooth Scale"></u-cell-item> -->
					<!-- <u-cell-item @click="printTest()"  title="All-in-One Machine"></u-cell-item>
					<u-cell-item @click="printTest1()"  title="Old All-in-One Machine"></u-cell-item>
					 -->
				</u-cell-group>
			</view>
			<view class="u-m-t-20" v-if="!onupdates">
				<u-cell-group>
					<u-cell-item @click="openSettings()"  title="Location Authorization"></u-cell-item>
				</u-cell-group>
				<u-cell-group>
					<u-cell-item @click="jumpOld()"  title="Bluetooth Connection"></u-cell-item>
				</u-cell-group>
			</view>
			<view class="u-m-t-20">
				<u-cell-group>
					<u-cell-item icon="edit-pen" @click="changePwd()"  title="Change Password"></u-cell-item>
				</u-cell-group>
				<u-cell-group>
					<u-cell-item icon="edit-pen" @click="baseInfo()"  title="Basic Information"></u-cell-item>
				</u-cell-group>
				<u-cell-group>
					<u-cell-item @click="LogOut()"  title="Log Out"></u-cell-item>
				</u-cell-group>
			</view>
			
		</view>
		<u-popup v-model="popupShow" mode="center" closeable width="500rpx" height="500rpx" border-radius="10">
			<view class="u-text-center u-m-t-20">Change Password</view>
			<view class=" u-m-20">
				<u-input v-model="form.oldPassword" type="password" border="true"  placeholder="Please enter old password" />
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.newPassword" type="password" border="true"  placeholder="Please enter new password" />
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.repeatPassword" type="password" border="true"  placeholder="Please re-enter new password" />
			</view>
			<view  style="height: 80rpx;text-align: center;">
				<view class="close-btn u-m-t-40">
					<u-button size="medium" @click="define()">Confirm</u-button>
				</view>
			</view>
		</u-popup>
		<view>
			<u-toast ref="uToast" />
		</view>
		<u-tabbar :list="tabbar" :active="active" @change="changeTab"></u-tabbar>
	</view>
</template>

<script>
	import config from '../../config'
	export default {
		data() {
			return {
				onupdates:false,
				title: 'My',
				tabbar: '',
				avatar:'',
				show:true,
				username:'',
				name:"",
				form:{
					oldPassword:'',
					newPassword:"",
					repeatPassword:"",
				},
				popupShow:false,
				print:false
			};
		},
		onLoad() {
			this.title = 'My'; // Set page title to English

			this.tabbar =this.$store.state.tabbar;
			// Translate tabbar titles to English
			this.tabbar.forEach(item => {
				if (item.text === '我的') {
					item.text = 'My';
				} else if (item.text === '首页') {
					item.text = 'Home';
				}
			});
			
			//#ifdef APP-PLUS
			this.onupdates = true
			//#endif
		},
		onShow() {
			this.$http.post('/rest/auth/me',{
			},{
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				const data = res.data.data
				this.avatar = data.avatar
				this.username = data.account
				this.name = data.name
			}).catch(err => {
				let message = err.data.message;
				this.$refs.uToast.show({
					title: 'Login expired, please log in again',
					type: 'error', 
					position:'bottom'
				})
				setTimeout(() => {
					uni.removeStorage({
						key: 'token',
						success: function (res) {
							
						},
						complete:function(res){
							uni.reLaunch({
								url: 'pages/login/login'
							});
						}
					});
				}, 500)
			});
		},
		computed: {
			avatars() {
				if(this.avatar != "" && this.avatar != null){
					return config.root + 'system/previewa/' + this.avatar
				} else {
					return ""
				}
			}
			
		},
		methods: {
			jumpOld(){
				uni.navigateTo({
					url: './print'
				})
			},			
			lydzc(){
				uni.navigateTo({
					url: './lydzc'
				})
			},
			// printTest(){
			// 	uni.navigateTo({
			// 		url: './printTest'
			// 	})
			// },
			// printTest1(){
			// 	uni.navigateTo({
			// 		url: './printTest1'
			// 	})
			// },
			openSettings(){
				wx.openSetting()
			},
			define(){
				if(typeof this.form.oldPassword == 'undefined' || this.form.oldPassword == null || this.form.oldPassword == ""){
					uni.showToast({
						title: "Please enter old password",
						icon: 'none'
					})
					return
				}
				if(typeof this.form.newPassword == 'undefined' || this.form.newPassword == null || this.form.newPassword == ""){
					uni.showToast({
						title: "Please enter new password",
						icon: 'none'
					})
					return
				}
				if(typeof this.form.repeatPassword == 'undefined' || this.form.repeatPassword == null || this.form.repeatPassword == ""){
					uni.showToast({
						title: "Please re-enter new password",
						icon: 'none'
					})
					return
				}
				let verify = /^[\S]{6,12}$/;
				if(!verify.test(this.form.newPassword)){
					uni.showToast({
						title: "Password must be 6-12 characters without spaces",
						icon: 'none'
					})
					return
				}
				if(this.form.newPassword !== this.form.repeatPassword){
					uni.showToast({
						title: "Passwords do not match",
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: 'Submitting...'
				})
				this.$http.post('/rest/auth/changePwd',{
				},{
					params: this.form,
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if(res.data.code == 200) {
						this.$refs.uToast.show({
								title: 'Password changed successfully',
								type: 'success'
							})
						setTimeout(() => {
							uni.removeStorage({
							    key: 'token',
							    success: function (res) {
									
							    },
								complete:function(res){
									uni.reLaunch({
									    url: 'pages/login/login'
									});
								}
							});
						}, 1000)
						this.popupShow = false
					} else {
						this.$refs.uToast.show({
								title: res.data.message,
								type: 'error'
							})
					}
				})
			},
			changePwd(){
				this.popupShow = true
			},
			baseInfo(){
				uni.navigateTo({
					url: './baseInfo'
				})
			},
			LogOut(){
				uni.showModal({
				    title: 'Confirm',
				    content: 'Are you sure you want to log out?',
				    confirmText: 'Confirm',
				    cancelText: 'Cancel',
				    success: function (res) {
				        if (res.confirm) {
							//#ifdef MP-WEIXIN
							let appYingyanTimeId = uni.getStorageSync('appYingyanTimeId')
							if(typeof appYingyanTimeId != 'undefined' && appYingyanTimeId != null && appYingyanTimeId != ''){
								wx.stopLocationUpdate({
									success: (res) => {
										console.log('关闭监听实时位置变化成功:',res)
									},
									fail: (res) => {
										console.log('关闭监听实时位置变化失败:',res)
									}
								})
								clearInterval(appYingyanTimeId)
								uni.removeStorageSync('appYingyanTimeId')
								uni.removeStorageSync('appYingyanDispatchNumber')
								uni.removeStorageSync('userInfo')//销毁缓存数据
								uni.removeStorageSync('HxStorageOutParam')//销毁缓存数据
								uni.removeStorageSync('HxParam')//销毁缓存数据
								
							}
							//#endif
							uni.removeStorage({
							    key: 'token',
							    success: function (res) {
									
							    },
								complete:function(res){
									uni.reLaunch({
									    url: 'pages/login/login'
									});
								}
							});
				            
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			onupdatess(){
				this.$http.post('/rest/sysJiluappVersions/newVersions',{//查询app最新版本
				},{
					params: {},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let data = res.data.data
					if(data != ''){
						if(config.versions != data.version){
							uni.showModal({
								title: "New version "+data.version+", update now?",
								content: "Update content: "+data.nodes,
								success: (res) => {
									if (res.confirm == true) {//当用户确定更新，执行更新
										this.doUpData();
									}
								}
							})
						} else {
							uni.showToast({
									title: "Already the latest version "+data.version,
									icon: 'none'
								})
						}
					}
				})
			},
			doUpData(){
				uni.showLoading({
					title: 'Updating...'
				})
				uni.downloadFile({//执行下载
					url: config.root+'appDownload/download', //下载地址
					success: downloadResult => {//下载成功
						uni.hideLoading();
						if (downloadResult.statusCode == 200) {
							uni.showModal({
								title: '',
								content: 'Update successful. Restart now?',
								confirmText: 'Restart',
								confirmColor: '#EE8F57',
								success: function(res) {
									if (res.confirm == true) {
										plus.runtime.install(//安装
											downloadResult.tempFilePath, {
												force: true
											},
											function(res) {
												utils.showToast('更新成功，重启中');
												plus.runtime.restart();
											}
										);
									}
								}
							});
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #ededed;
	}

	.camera{
		width: 54px;
		height: 44px;
		
		&:active{
			background-color: #ededed;
		}
	}
	.user-box{
		background-color: #fff;
	}
</style>
